package org.nepu.sc.math;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author kindear
 */
@Slf4j
public class MathUtil {
    public static double[][] build2Vec(List<Integer> list){
        int len = list.size();
        double[][] mx = new double[len][len];
        for (int i = 0; i < len; i++){
            for (int j = 0;j < len;j++){
                if(j >= i){
                    int x = list.get(i);
                    int y = list.get(j);
                    if (x == y){
                        mx[i][j] = 1;
                    }
                    else {
                        //
                        int []t = new int[]{1,3,5,7,9};
                        if (y < x){
                            mx[i][j] = t[Math.abs(y - x) / 2];
                        }else {
                            mx[i][j] = (double) 1 / t[Math.abs(y - x) / 2];
                        }
                    }
                }else {
                    mx[i][j] = 1/mx[j][i];
                }
            }
        }
        return mx;
    }
    // 方法：计算每行的和
    public static double[] calculateRowSums(double[][] array) {
        int rows = array.length; // 行数
        int cols = array[0].length; // 列数

        // 用于存储每行的和
        double[] rowSums = new double[rows];

        // 迭代每一行
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                rowSums[row] += array[row][col]; // 累加行中每个元素的值
            }
        }

        return rowSums;
    }
    // 方法：计算每列的和
    public static double[] calculateColumnSums(double[][] array) {
        int rows = array.length; // 行数
        int cols = array[0].length; // 列数

        // 用于存储每列的和
        double[] columnSums = new double[cols];

        // 迭代每一列
        for (int col = 0; col < cols; col++) {
            for (int row = 0; row < rows; row++) {
                columnSums[col] += array[row][col]; // 累加列中每个元素的值
            }
        }

        return columnSums;
    }

    public static double[] buildMx(List<Integer> list){
        int len = list.size();
        double[][] mx = build2Vec(list);
        double[][] rmx = new double[len][len];
        double csum[] = calculateColumnSums(mx);
        for (int i = 0; i < len; i++){
            for (int j = 0; j < len;j++){
                rmx[i][j] = mx[i][j] / csum[j];
            }
        }
        double[] rsums = calculateRowSums(rmx);
        double[] result = new double[len];
        for (int i=0;i<len;i++){
            result[i] = rsums[i]/len;
        }
        return result;
    }


    public static double[] buildMx(int[] list){
        int len = list.length;
        double[][] mx = build2Vec(list);
        double[][] rmx = new double[len][len];
        double csum[] = calculateColumnSums(mx);
        for (int i = 0; i < len; i++){
            for (int j = 0; j < len;j++){
                rmx[i][j] = mx[i][j] / csum[j];
            }
        }
        double[] rsums = calculateRowSums(rmx);
        double[] result = new double[len];
        for (int i=0;i<len;i++){
            result[i] = rsums[i]/len;
        }
        return result;
    }
    public static double[][] build2Vec(int[] list){
        int len = list.length;
        double[][] mx = new double[len][len];
        for (int i = 0; i < len; i++){
            for (int j = 0;j < len;j++){
                if(j >= i){
                    int x = list[i];
                    int y = list[j];
                    if (x == y){
                        mx[i][j] = 1;
                    }
                    else {
                        //
                        int []t = new int[]{1,3,5,7,9};
                        if (y < x){
                            mx[i][j] = t[Math.abs(y - x) / 2];
                        }else {
                            mx[i][j] = (double) 1 / t[Math.abs(y - x) / 2];
                        }
                    }
                }else {
                    mx[i][j] = 1/mx[j][i];
                }
            }
        }
        return mx;
    }

    /**
     * 一维矩阵乘以二维矩阵
     * @param matrix
     * @param w
     * @return
     */
    public static double[] multi(double[][] matrix ,double[] w){
        log.info(String.valueOf(matrix[0].length));
        double[] bw = new double[matrix[0].length];
        for(int i=0;i<matrix[0].length;i++){
            for(int j=0;j<matrix.length;j++){
                bw[i]+=matrix[j][i]*w[j];
            }
        }
        return bw;
    }
    public static void main(String[] args) {
        int list[] = new int[]{1,3,7,1,3};
        double[] result = buildMx(list);
        double sum = 0.0;
        for (int i=0;i<result.length;i++){
            sum =  sum+ result[i];
        }
        log.info(String.valueOf(sum));
        // log.info(Arrays.toString(result));
    }
}
